/**
 * 
 */
import java.io.*;
import java.util.Enumeration;
import java.util.Hashtable;

/**
 * <b>Description:</b><br/> This class receives the user input and subsequently constructs 
 * 		a crosswalk from it i.e. user enters a RIF-CS element for every corresponding
 * 		XML element. This mapping is stored in the hashtable, elementMap (from ParseSchema.java) 
 * 
 * <br/><br/><b>Last modified:</b><br/>
 * 
 * @author Sunny Toor
 * @param n/a
 * @return n/a
 * @exception File IO Exception
 * 
 */
public class SaveDisplayMapping {

	/** class that stores the mapping */
	private ParseSchema schema = new ParseSchema();
	
	/**
	 * <b>Description:</b><br/> constructor that initialises the class
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 30th July 2011
	 * 
	 * @author Sunny Toor
	 * @param n/a
	 * @return n/a
	 * @exception n/a
	 * 
	 */
	public SaveDisplayMapping() {
		
	}

	/**
	 * <b>Description:</b><br/> Construct one half of the mapping i.e. using ParseSchema.java 
	 * 		and then get the user input to map the XML elements to corresponding RIF-CS elements.
	 * 		Then store the user input (containing RIFCS elements) into the same 'elementMap' data-structure 
	 * 		that contains elements of the source XML schema. Thereby, creating a crosswalk. 
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 30th July 2011
	 * 
	 * @author Sunny Toor
	 * @param Output stream
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	public void constructElementMapping(PrintStream out) throws Exception{
		schema.traverseDoc(schema.getDocReprensation("mods-schema.xml"), System.out);
		Hashtable<String,String> myMap = schema.getElementMap();
		//BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("asdf.dst")));
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		
		for(Enumeration<String> e = myMap.keys(); e.hasMoreElements(); ){
			String myKey = e.nextElement();
			out.print(myKey+" - ");
			String val = in.readLine();
			schema.getElementMap().put(myKey, val);
		}
	}
	
	/**
	 * <b>Description:</b><br/> The crosswalk created in <i>'constructElementMapping'</i> is then saved in a file
	 * 		named "mapping.txt". This file can later be used in other parts of the tool.
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 30th July 2011
	 * 
	 * @author Sunny Toor
	 * @param n/a
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	public void saveElementMapping() throws Exception{
		BufferedWriter w = new BufferedWriter(new FileWriter("mapping.txt"));
		Hashtable<String,String> localMap = schema.getElementMap();
		for(Enumeration<String> e = localMap.keys(); e.hasMoreElements();){
			String k = e.nextElement();
			String v = localMap.get(k);
			w.write(k+" -> "+v);
			w.newLine();
		}
		w.close();
	}
	
	/**
	 * <b>Description:</b><br/> A testing method to display the contents of the mapping/crosswalk 
	 * 		on the screen.
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 30th July 2011
	 * 
	 * @author Sunny Toor
	 * @param Output stream
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	public void displayMapping(PrintStream out) throws Exception{
		Hashtable<String,String> localMap = schema.getElementMap();
		for(Enumeration<String> e = localMap.keys(); e.hasMoreElements();){
			String k = e.nextElement();
			out.println(k+" -> "+localMap.get(k));
		}
	}
	
	/**
	 * <b>Description:</b><br/> Main method that drives the entire class. 
	 * 
	 * <br/><br/><b>Last modified:</b><br/> 30th July 2011
	 * 
	 * @author Sunny Toor
	 * @param args
	 * @return n/a
	 * @exception File IO Exception
	 * 
	 */
	public static void main(String[] args) throws Exception {
		SaveDisplayMapping m = new SaveDisplayMapping();
		m.constructElementMapping(System.out);
		m.saveElementMapping();
		//m.displayMapping(System.out);
	}

}
